package com.geek.gkpush.framework.threadpool.util;

import com.geek.gkpush.util.json.JacksonUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * <h1>获取线程池信息的工具类。</h1><br/>
 *
 * <h1><br/><a href="https://u.geekbang.org/subject/arch3/100614201">Java 业务架构实战营：https://u.geekbang.org/subject/arch3/100614201</a></h1><br/><h1><a href="https://gitee.com/ylimhhmily/BizArchPracticeCamp">Java 业务架构实战营代码地址：https://gitee.com/ylimhhmily/BizArchPracticeCamp</a></h1><br/><h1><a href="https://u.geekbang.org/subject/java4th/1001148?source=app_share">高级 Java 工程体系课 2023 版：https://u.geekbang.org/subject/java4th/1001148?source=app_share</a></h1><br/><h1><a href="https://time.geekbang.org/column/intro/100312101?tab=catalog">Dubbo 源码剖析与实战：https://time.geekbang.org/column/intro/100312101?tab=catalog</a></h1>
 *
 * @author hmilyylimh
 *         ^_^
 * @version 0.0.1
 *         ^_^
 * @date 2023/10/2
 *
 */
public class ExecutorInfoUtils {

    /**
     * <h2>获取线程池的一些简单基本信息。</h2>
     *
     * @param threadPoolExecutor
     * @return
     */
    public static String getExecutorInfo(String threadName, ThreadPoolExecutor threadPoolExecutor) {
        Map<String, Object> map = new HashMap<>(16);

        map.put("threadName", threadName);
        map.put("poolSize", threadPoolExecutor.getPoolSize());
        map.put("corePoolSize", threadPoolExecutor.getCorePoolSize());
        map.put("largestPoolSize", threadPoolExecutor.getLargestPoolSize());
        map.put("maximumPoolSize", threadPoolExecutor.getMaximumPoolSize());

        map.put("queueCapacity", threadPoolExecutor.getQueue().size());

        map.put("activeCount", threadPoolExecutor.getActiveCount());
        map.put("taskCount", threadPoolExecutor.getTaskCount());
        map.put("completedTaskCount", threadPoolExecutor.getCompletedTaskCount());

        map.put("isShutdown", threadPoolExecutor.isShutdown());
        map.put("isTerminated", threadPoolExecutor.isTerminated());
        map.put("isTerminating", threadPoolExecutor.isTerminating());

        return JacksonUtils.toJson(map);
    }
}